package objects;

import java.util.ArrayList;

import world.BodyID;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.tiled.TiledMap;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;

public class Tower extends AbstractObject {

	private boolean facingRight;
	private long lastProjectileTime;
	private ArrayList<Projectile> projectiles = new ArrayList<Projectile>();

	public Tower(TiledMap tiledMap, World _world, int posX, int posY,
			boolean _facingRight) {

		// TODO Auto-generated constructor stub
		this.type = BodyDef.BodyType.StaticBody;
		facingRight = _facingRight;

		sprite = new Sprite(new Texture(
				Gdx.files.internal("data/sprites/tower.png")));
		sprite.flip(facingRight, false);
		spriteBatch = new SpriteBatch();
		// this.position
		// .set((posX + tiledMap.tileWidth / 2) / PIXELS_PER_METER,
		// (tiledMap.height * tiledMap.tileHeight - (posY - tiledMap.tileHeight
		// / 2))
		// / PIXELS_PER_METER);
		this.position.set(((posX + sprite.getWidth() / 2) / PIXELS_PER_METER),
				(tiledMap.height * tiledMap.tileHeight - (posY))
						/ PIXELS_PER_METER);

		world = _world;
		body = world.createBody(this);
		body.setActive(true);
		body.setFixedRotation(true);
		if (facingRight) {
			body.setUserData(BodyID.TOWER_RIGHT);
		} else {
			body.setUserData(BodyID.TOWER_LEFT);
		}

		PolygonShape shape = new PolygonShape();
		shape.setAsBox(sprite.getWidth() / (2 * PIXELS_PER_METER),
				sprite.getHeight() / (2 * PIXELS_PER_METER));

		FixtureDef fixtureDef = new FixtureDef();
		fixtureDef.shape = shape;
		body.createFixture(fixtureDef);

		lastProjectileTime = System.currentTimeMillis();
	}

	@Override
	public void renderObject(Camera _camera, SpriteBatch _spritebatch) {
		if (body.isActive()) {
			fireProjectile();
			System.out.println(projectiles.size());
			for (int i = 0; i < projectiles.size(); i++) {
				if (projectiles.get(i).active) {
					projectiles.get(i).renderObject(_camera, _spritebatch);
				} else {
					projectiles.remove(i);
					world.destroyBody(body);
				}
			}
			sprite.setPosition(
					PIXELS_PER_METER * body.getPosition().x - sprite.getWidth()
							/ 2, PIXELS_PER_METER * body.getPosition().y
							- sprite.getHeight() / 2);
			sprite.draw(_spritebatch);

		}
	}

	public float getProjectileStartX() {
		if (facingRight) {
			return this.position.x + (sprite.getWidth() / 4) / PIXELS_PER_METER;
		} else {
			return this.position.x /*- (sprite.getWidth() / 2)/PIXELS_PER_METER*/;
		}
	}

	public float getProjectileStartY() {

		return this.position.y /*- (sprite.getHeight() / 2)/PIXELS_PER_METER*/;
	}

	public void fireProjectile() {
		if (System.currentTimeMillis() - lastProjectileTime >= 3000) {

			projectiles.add(new Projectile(world, this, facingRight));
			lastProjectileTime = System.currentTimeMillis();
		}

	}
}
